package com.ylbh.modules.member.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.member.dto.RevenueLogDTO;
import com.ylbh.common.pojo.member.dto.select.RevenueLogSelectDTO;
import com.ylbh.common.pojo.member.vo.IntegralOrderVO;
import com.ylbh.common.pojo.member.vo.RevenueLogDetailsVO;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.member.mapper.RevenueLogMapper;
import com.ylbh.modules.member.service.RevenueLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消费者列表
 */
@Service("revenueLogService")
public class RevenueLogServiceImpl extends ServiceImpl<RevenueLogMapper, RevenueLogDTO> implements RevenueLogService {
    @Autowired
    private RevenueLogMapper revenueLogMapper;

    /**
     * 添加购物券日志
     * @param revenueLogDTO
     */
    @Override
    public void add(RevenueLogDTO revenueLogDTO) {
        revenueLogMapper.insert(revenueLogDTO);
    }

    @Override
    public PageUtils queryRevenueLogList(RevenueLogSelectDTO params) {
        Page<Object> page = new Page<>(params.getPage(), params.getLimit());
        List<RevenueLogDTO> logList = revenueLogMapper.selectRevenueLogList(params, page);
        return new PageUtils(new Page<RevenueLogDTO>().setTotal(page.getTotal()).setRecords(logList));
//        return new PageUtils(revenueLogMapper.selectRevenueLogList(params),
//                revenueLogMapper.getRevenueLogCount(params),
//                params.getLimit(),
//                params.getPage());
    }

    /**
     * 用户现有券数量
     * @param name
     * @return
     */
    @Override
    public Integer queryExistingCoupons(String name) {
        return revenueLogMapper.selectExistingCoupons(name);
    }

    /**
     * 系统管理购物券明细
     * @param params
     * @return
     */
    @Override
    public PageUtils queryAdminIntegralLogList(RevenueLogSelectDTO params) {
        Page<Object> page = new Page<>(params.getPage(), params.getLimit());
        List<RevenueLogDTO> list = revenueLogMapper.selectAdminIntegralLogList(params, page);
        return new PageUtils(new Page<RevenueLogDTO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 购物券明细详情
     * @param id
     * @return
     */
    @Override
    public RevenueLogDetailsVO getRevenueLogDetails(Long id) {
        RevenueLogDetailsVO vo = revenueLogMapper.selectRevenueLogDetails(id);
        List<IntegralOrderVO> list = null;
        // 根据类型和单号查询出订单信息
        if (null != vo.getRelationType() && vo.getRelationType() == 1) {
            list = revenueLogMapper.selectOrderByTypeOrOrderNo(vo.getRelationNo(), 1);
        } else if (null != vo.getRelationType() && vo.getRelationType() == 2) {
            list = revenueLogMapper.selectOrderByTypeOrOrderNo(vo.getRelationNo(), 2);
        }
        vo.setList(list);
        return vo;
    }
}
